#!/usr/bin/env python3
#coding=utf-8

import re
from urllib.request import urlopen
from urllib.parse import quote

keyword = "狗粮"
url = "https://search.jd.com/Search?keyword={0}&enc=utf-8".format(quote(keyword))
html = urlopen(url).read().decode('utf-8')
regex = re.compile(r'<li data-sku=".*" class="gl-item">[\s\S]*?</li>')
patt = re.findall(regex, html)
title_regex = re.compile(r'<a .* title="(.*)" href=.*[\s\S]*?</a>')
href_regex = re.compile(r'<a .*? href="(.*)" onclick=.*[\s\S]*?</a>')
price_regex = re.compile(r'data-price="(.*)"')

for i in patt:
    print("商品名称：" + re.findall(title_regex, i)[0])
    print("商品链接：" + re.findall(href_regex,i)[0].split('//')[1])
    print("商品价格：" + re.findall(price_regex,i)[0] + "元")
    print("----------我是分割线----------")
